//
//  WSJPickView.m
//  MyPickView
//
//  Created by wsj2015 on 16/1/25.
//  Copyright © 2016年 wsj2015. All rights reserved.
//

#import "WSJPickView.h"
#import <YTXAnimations/UIView+YTXAnimateCSS.h>
#import "Masonry.h"
#import "WSJMaskView.h"

@interface WSJPickView ()<UIPickerViewDelegate,UIPickerViewDataSource>


@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UIPickerView *picker;

@property (nonatomic,  strong) UIWindow *pickWindow;


@property (nonatomic, strong) WSJMaskView *maskView;


@property (nonatomic, copy) NSString *selectString;
@property (nonatomic,  assign) NSInteger index;

@end

@implementation WSJPickView

- (IBAction)cancelBtn:(id)sender {
    [self dissmiss];
    
}

- (IBAction)savebtn:(id)sender {
    if (!self.selectString) {
        return;
    }
    if (self.saveBlock) {
        self.saveBlock(self.selectString,self.index);
    }
    [self dissmiss];
}


+(instancetype)pickView{
   
    return [[[NSBundle mainBundle] loadNibNamed:@"WSJPickView" owner:nil options:0]lastObject];

}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        
        
    }
    return self;

}
-(UIView *)maskView{
    if (_maskView ==nil) {
        _maskView =[[WSJMaskView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height )];
//        _maskView.backgroundColor =[UIColor blackColor];
        
      
        
//        _maskView.alpha =0;
        defineWeakSelf;
        _maskView.tapBlock =^(CGPoint point){
        
            [weakSelf dissmiss];;
        };
        
    }
    return _maskView;

}
- (void)show{
//    CGSize screen =[UIScreen mainScreen].bounds.size;
    

    
    if (!self.pickWindow) {
        UIWindow *window =[[UIWindow  alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)] ;
        
        self.picker.dataSource = self;
        self.picker.delegate =self;
        window.windowLevel = UIWindowLevelAlert;
        [window addSubview:self.maskView];
        [window addSubview:self];
        
        [window becomeKeyWindow];
        [window makeKeyAndVisible];
        self.pickWindow = window;
        
//        self.picker.backgroundColor = kAppBg;
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(250);
            make.left.mas_equalTo((kScreenWidth-300)/2);
            make.top.mas_equalTo((kScreenHeight -250)/2);
        }];
//        [self.titleLabel ytx_bounceInAnimtionWithDurationTime:1.0f];
//        [self.picker ytx_zoomInDownAnimtionWithDurationTime:1.0f];
//
        self.alpha= 0;
        CGRect frame = self.frame;
        frame.origin.y =self.maskView.bounds.size.height-250;
        [UIView animateWithDuration:.35f delay:0 usingSpringWithDamping:.6 initialSpringVelocity:.4 options:UIViewAnimationOptionLayoutSubviews animations:^{
           
//            self.frame =frame;
            self.alpha =1;
//            [self ytx_lightSpeedInAnimtionWithDurationTime:.35f];
//            self.maskView.alpha = .6f;
            self.layer.cornerRadius =8;
            self.layer.masksToBounds = YES;
        } completion:^(BOOL finished) {
        
            
            
        }];
    }
 
  

}

- (void)dissmiss{
    
//    [self ytx_zoomOutAnimtionWithDurationTime:.4f];
  
  
    [UIView animateWithDuration:.35f animations:^{
       
        self.alpha = 0;
      
        self.maskView.alpha =0;
        
    } completion:^(BOOL finished) {
        
       
        [self.maskView removeFromSuperview];
       
        [self.pickWindow resignKeyWindow];
        self.pickWindow =nil;
         [self removeFromSuperview];
        
        
    }];
    
    

}


- (void)dealloc{
    
    

}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray =dataArray;
    if (dataArray.count>0) {
        self.selectString = dataArray[0];
    }
    [self.picker reloadComponent:0];

}
- (void)setPickTitle:(NSString *)pickTitle
{
    _pickTitle =pickTitle;
    self.titleLabel.text = pickTitle;


}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
    
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (!self.dataArray) {
        return 0;
    }
    return self.dataArray.count;
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 30;
    
}
- (CGSize)rowSizeForComponent:(NSInteger)component{
    return CGSizeMake(100, 30);
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.dataArray[row];
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.dataArray.count ==0) {
        return;
    }
    self.index =row;
    self.selectString = self.dataArray[row];



}
@end
